﻿using DisplayReading.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DisplayReading
{
    /// <summary>
    /// ucDispaly.xaml 的交互逻辑
    /// </summary>
    public partial class ucDispaly : UserControl
    {
        /// <summary>
        /// 数据
        /// </summary>
        public ucDispalyData data { get; set; }
        /// <summary>
        /// 测试显示器
        /// </summary>
        List<DisplayData> displayList_text { get; set; }
        public ucDispaly()
        {
            InitializeComponent();

            this.Loaded += UcDispaly_Loaded;
        }
        private void textfun()
        {
            displayList_text = new List<DisplayData>()
            {
                new DisplayData()
                {
                    name="1111"
                },
                new DisplayData()
                {
                    name="2222"
                }
            };
            data.displayList = displayList_text;
        }

        private void UcDispaly_Loaded(object sender, RoutedEventArgs e)
        {
            data = DataContext as ucDispalyData;
            var hostname = Dns.GetHostName();
            var ips = Dns.GetHostAddresses(hostname);
            var locahostIp = "";
            foreach (var ip in ips)
            {
                Console.WriteLine(ip.ToString() + " " + ip.AddressFamily.ToString());
                if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    locahostIp += ip.ToString() + " ";
                }
            }
            data.locahostIp = locahostIp.Trim();

            //textfun();
        }

        private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Label lb = sender as Label;
            if (lb != null)
            {
                foreach (var item in data.displayList)
                {
                    if (item.name == lb.Content.ToString())
                    {
                        item.ischeck = !item.ischeck;
                    }
                    else
                    {
                        if (item.ischeck)
                        {
                            item.ischeck = false;
                        }
                    }
                }
            }
        }

        public void SetPreview(BitmapImage bit)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                preview.Source = bit;
            }));
        }
    }
}
